package com.whitecat.voting.web;

import com.whitecat.voting.common.Resp;
import com.whitecat.voting.service.VotingRecordService;
import com.whitecat.voting.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "投票接口", tags={"投票接口"})
@Slf4j
@RestController
@RequestMapping("/voting/")
public class VotingController extends BaseController {
    private final VotingRecordService votingRecordService;

    public VotingController(VotingRecordService votingRecordService) {
        this.votingRecordService = votingRecordService;
    }

    @ApiOperation(value = "投票操作", notes = "小程序用户投票操作。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "candidateId", value = "候选人openid", dataType = "string", paramType = "query")
    })
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public Resp<?> vote(@RequestParam String candidateId) {
        String votersId = ShiroUtils.getSignInUser().getOpenId();
        return votingRecordService.add(candidateId, votersId) ? ok() : fail();
    }
}
